﻿using System;
using System.Threading;
using System.ComponentModel;
using System.Diagnostics;
using BaseDll;
namespace OtherDevice
{
    [Description("力准L400-B")]
    public class Pressure_L400B: Pressure_Base
    {
        public Pressure_L400B(string strName) : base(strName)
        {
            controlFrom = new Form_压力测试();
        }

        Object muxt = new Object();
        public override bool Get(ref int n_Value)
        {
            lock (muxt)
            {
                try
                {
                    byte[] byteArray = new byte[8];
                    byteArray[0] = 0x01;
                    byteArray[1] = 0x03;
                    byteArray[2] = 0x01;
                    byteArray[3] = 0xA4;
                    byteArray[4] = 0x00;
                    byteArray[5] = 0x02;
                    int n_CRCValue = ParityType.CRC16(byteArray, 6);
                    byteArray[6] = (byte)(n_CRCValue & 0X00FF);
                    byteArray[7] = (byte)((n_CRCValue & 0XFF00) >> 8);
                    byte[] S = new byte[9];
                    if (SendAndReciveData(byteArray, ref S, 500))
                    {
                        if (S[3] == 0)
                            n_Value = S[5] * 256 + S[6];
                        else
                            n_Value = S[5] * 256 + S[6] - 256 - 256 * 256;
                        return true;
                    }
                    else
                        return false;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show($"数据获取失败{ex}");
                    return false;
                }
            }
        }
        public override bool SetCompareValue(int n_Value)
        {
            lock (muxt)
            {
                try
                {
                    byte[] byteArray = new byte[13];
                    byteArray[0] = 0x01;
                    byteArray[1] = 0x10;
                    byteArray[2] = 0x00;
                    byteArray[3] = 0x02;
                    byteArray[4] = 0x00;
                    byteArray[5] = 0x02;
                    byteArray[6] = 0x04;
                    byteArray[7] = (byte)((n_Value & 0XFF000000) >> 24);
                    byteArray[8] = (byte)((n_Value & 0XFF0000) >> 16);
                    byteArray[9] = (byte)((n_Value & 0XFF00) >> 8);
                    byteArray[10] = (byte)(n_Value & 0X00FF);
                    int n_CRCValue = ParityType.CRC16(byteArray, 11);
                    byteArray[11] = (byte)(n_CRCValue & 0X00FF);
                    byteArray[12] = (byte)((n_CRCValue & 0XFF00) >> 8);
                    byte[] S = new byte[8];
                    return SendAndReciveData(byteArray, ref S, 500);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show($"数据获取失败{ex}");
                    return false;
                }
            }
        }
        public override void SetZore()
        {
            try
            {
                byte[] byteArray = new byte[8];
                byteArray[0] = 0x01;
                byteArray[1] = 0x05;
                byteArray[2] = 0x01;
                byteArray[3] = 0xCE;
                byteArray[4] = 0xFF;
                byteArray[5] = 0x00;
                int n_CRCValue = ParityType.CRC16(byteArray, 6);
                byteArray[6] = (byte)(n_CRCValue & 0X00FF);
                byteArray[7] = (byte)((n_CRCValue & 0XFF00) >> 8);
                byte[] S = null;
                 SendAndReciveData(byteArray, ref S, 500);
                return;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"数据获取失败{ex}");
                return;
            }
        }
    }
}
